package com.aierkeji.b2c.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.aierkeji.b2c.entity.AkjOrderRecord;
import com.aierkeji.b2c.entity.snapshot.AkjAlipayLifeSnap;
import com.aierkeji.b2c.utils.AppConfig;
import com.aierkeji.b2c.utils.StringUtils;
import com.aierkeji.b2c.utils.sms.SendSms;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class AsyncService {
	private final Logger log = LogManager.getLogger(getClass());

	@Async("b2cAsyncServiceExecutor")
	public void sendOrderSms(AkjOrderRecord obj, String templete, AkjAlipayLifeSnap akjAlipayLife) {
		log.info("start executeAsync");
		if ("dev".equals(AppConfig.getProperty("envType"))) {
			log.info("in dev");
			return;
		}
		try {
			if (obj != null && !com.aierkeji.b2c.utils.StringUtils.isEmpty(obj.getLifeId())) {
				if (akjAlipayLife != null) {
					String tel = akjAlipayLife.getTel();
					SendSms sendMsg = new SendSms(obj.getBorrowTime(), obj.getClient(), obj.getProductName(),
							obj.getLastActivationTime(), obj.getDeposit(), obj.getRefundPremium(), obj.getOrderNo(),
							tel);
					Map<String, Object> map = sendMsg.getSmsMap();
					String content = AppConfig.getProperty(templete);
					content = "【" + akjAlipayLife.getName() + "】" + StringUtils.processTemplate(content, map);
					sendMsg.send(content, obj.getPhone());
				}
			}
		} catch (Exception e) {
			log.error("", e);
		}
		log.info("end executeAsync");
	}

	@Async("b2cAsyncServiceExecutor")
	public void sendYzm(AkjOrderRecord akjOrderRecord, String yzm, AkjAlipayLifeSnap akjAlipayLifeSnap) {
		log.info("start executeAsync");
		if ("dev".equals(AppConfig.getProperty("envType"))) {
			log.info("in dev");
			return;
		}
		try {
			SendSms sendMsg = new SendSms();
			Map<String,Object> map= new HashMap<String, Object>();
			map.put("yzm", yzm);
			map.put("orderNo", akjOrderRecord.getOrderNo());
			map.put("userName", akjOrderRecord.getClient());
			String content = AppConfig.getProperty("sms.cancel.yzm");
			content = "【" + akjAlipayLifeSnap.getName() + "】" + StringUtils.processTemplate(content, map);
			sendMsg.send(content, akjOrderRecord.getPhone());
		} catch (Exception e) {
			log.error("", e);
		}
		log.info("end executeAsync");
	}
}
